function loadCSS(url, callback) {
    setTimeout(function () {
        var link = document.createElement("link");
        link.type = "text/css";
        link.rel = "stylesheet";
        link.href = url;
        document.getElementsByTagName("body")[0].appendChild(link);
        if (link.readyState) {
            link.onreadystatechange = function () {
                if (link.readyState == "loaded" || link.readyState == "complete") {
                    link.onreadystatechange = null;
                    if (callback) {
                        callback();
                    }
                }
            }
        } else {
            link.onload = function () {
                if (callback) {
                    callback();
                }
            }
        }
    }, 0);
}

//special_right_menu_start
$(function () {
    var special_right_menu = $(".special_right_menu");
    if (!special_right_menu.length) { return; }
    special_right_menu.parent().addClass("special_container");
    loadCSS("//image.benlailife.com/SpecialJs/special_right_menu.css", function () {
        var threshold = $(".special_container").offset().top;
        window.fixtop = 0;
        window.fixleft = 1000;
        var posSwitch = function () {
            var top = $(window).scrollTop();
            var intleft = $(".special_container").offset().left;
            $(".special_right_menu").toggleClass("top_abs", top <= threshold);
            if (top <= threshold) {
                $(".special_right_menu").css("left", fixleft);
            }
            $(".special_right_menu").toggleClass("top_fixed", top > threshold);
            if (top > threshold) {
                $(".special_right_menu").css("left", intleft + fixleft);
            }
        }
        $(window).bind("scroll resize orientationchange", posSwitch);
    });
});
//special_right_menu_end

//click_to_toggle_start
$(function () {
    var duration = 800;
    var toggle = $("[benlai-effect=toggle]");
    if (!toggle.length) { return; }
    toggle.each(function () {
        var isOn = $(this).attr("effect-initial") == "on";
        $(this).find("[effect-toggle=slide-up], [effect-toggle=slide-content]").toggle(isOn);
        $(this).find("[effect-toggle=slide-down]").toggle(!isOn);
    });
    toggle.delegate("[effect-toggle=slide-down]", "click", function () {
        $(this).siblings("[effect-toggle=slide-content]").slideDown(duration, function () {
            $(this).siblings("[effect-toggle=slide-down]").hide().siblings("[effect-toggle=slide-up]").show();
        });
    });
    toggle.delegate("[effect-toggle=slide-up]", "click", function () {
        $(this).siblings("[effect-toggle=slide-content]").slideUp(duration, function () {
            $(this).siblings("[effect-toggle=slide-up]").hide().siblings("[effect-toggle=slide-down]").show();
        });
    });
});
//click_to_toggle_end

//fader_start
var Fader = function (param) {
    param = param || {};
    this.container = param.container || "#fader_container";
    this.fadeList = param.fadeList || "#fader";
    this.switchList = param.switchList || "#switch";
    this.prev = param.prev || "#prev";
    this.next = param.next || "#next";
    this.speed = param.speed || 400;
    this.duration = param.duration || 4000;
    this.switchOn = param.switchOn || "http://image.benlailife.com/adpicture/subject/alasijia/dianOn.png";
    this.switchOff = param.switchOff || "http://image.benlailife.com/adpicture/subject/alasijia/dian.png";

    this.index = 0;
    this.interval = null;
    this.fadeItem = $(this.fadeList).find("li");
    this.switchItem = $(this.switchList).find("li");

    this.init().run();
};

Fader.prototype = {
    init: function () {
        this.fadeItem.css("position", "absolute").hide().eq(0).show();
        this.switchItem.eq(0).find("img").attr("src", this.switchOn);
        this.bindEvent();
        return this;
    },
    run: function () {
        var fader = this;
        fader.interval = window.setInterval(function () {
            fader.fadeForward(1);
        }, fader.duration);
        return this;
    },
    stop: function () {
        window.clearInterval(this.interval);
        return this;
    },
    bindEvent: function () {
        var fader = this;
        $(fader.container).hover(
            function () {
                fader.stop();
            },
            function () {
                fader.run();
            }
        );
        $(fader.switchItem).live("click", function () {
            fader.fadeTo($(this).index());
        });
        $(fader.prev).click(function () {
            fader.fadeForward(-1);
        });
        $(fader.next).click(function () {
            fader.fadeForward(1);
        });
        return this;
    },
    fadeForward: function (direction) {
        var fader = this;
        fader.index = fader.index + direction;
        fader.index = fader.index === fader.fadeItem.length ? 0 : fader.index;
        fader.index = fader.index === -1 ? fader.fadeItem.length - 1 : fader.index;
        fader.fadeTo(fader.index);
    },
    fadeTo: function (index) {
        var fader = this;
        fader.fadeItem.eq(index).stop(true, true).fadeIn(fader.speed).siblings().stop(true, true).fadeOut();
        fader.switchItem.find("img").attr("src", fader.switchOff);
        fader.switchItem.eq(index).find("img").attr("src", fader.switchOn);
        fader.index = index;
    }

};
//fader_end

//btn_hover_start
$(function () {
    var btn_hover = $(".btn_hover");
    if (!btn_hover.length) return;
    btn_hover.each(function () {
        var height = $(this).height();
        $(this).css({ "overflow": "hidden", "position": "relative" });
        $(this).find(".pic").css({ "position": "absolute", "z-index": 2 });
        $(this).find(".buy").css({ "position": "absolute", "z-index": 3, "top": height });
    });
    
    $(".btn_hover").live("mouseenter", function () {
        var height = $(this).height();
        var btn_height = $(this).find(".buy img").eq(0).height();
        $(this).find(".pic").stop(true, false).animate({ "top": -btn_height / 2, "opacity": "1" }, 300);
        $(this).find(".buy").stop(true, false).animate({ "top": height - btn_height }, 200);
    });

    $(".btn_hover").live("mouseleave", function () {
        var height = $(this).height();
        $(this).find(".pic").stop(true, false).animate({ "top": "0px", "opacity": "1" }, 200);
        $(this).find(".buy").stop(true, false).animate({ "top": height }, 300);
    });
});
//btn_hover_end

//tab_plugin_start
$(function () {
    $.fn.Tab = function () {
        $(this).each(function () {
            var $module = $(this);
            var tab_index = 0;
            var interval;

            var $nav_children = $module.find("[effect-tab=nav]").children();
            var len = $nav_children.length;

            var autoplay = $module[0].hasAttribute("autoplay") && len > 1;
            var hoverstop = $module[0].hasAttribute("hoverstop");
            var hover = $module[0].hasAttribute("hover");

            function run() {
                interval = setTimeout(function () {
                    switchForward(1);
                }, 4000);
            }

            function stop() {
                clearTimeout(interval);
            }

            function switchForward(step) {
                tab_index = tab_index + step;
                tab_index = tab_index > len - 1 ? 0 : tab_index;
                tab_index = tab_index < 0 ? len - 1 : tab_index;
                switchTo(tab_index);
            }

            function switchTo(index) {
                stop();

                $nav_children.removeClass("on").eq(index).addClass("on");
                $nav_children.find("[effect-status=off]").show().siblings("[effect-status=on]").hide();
                $nav_children.eq(index).find("[effect-status=on]").show().siblings("[effect-status=off]").hide();
                autoplay || hover ? $module.find("[effect-tab=content]").children().eq(index).stop(true, true).fadeIn(400).siblings().fadeOut(400) : $module.find("[effect-tab=content]").children().eq(index).show().siblings().hide();

                tab_index = index;
                autoplay && run();
            }

            $module.find("[effect-tab=nav]").children().click(function () {
                switchTo($(this).index());
            });

            $module.find("[effect-tab=prev]").click(function () {
                switchForward(-1);
            });

            $module.find("[effect-tab=next]").click(function () {
                switchForward(1);
            });

            hover && $module.find("[effect-tab=nav]").children().mouseenter(function () {
                switchTo($(this).index());
            });

            hoverstop && $module.find("[effect-tab=nav], [effect-tab=content]").hover(function () {
                stop();
            }, function () {
                run();
            });

            switchTo($module.attr("dafault-index") || 0);
        });
    }
});
//tab_plugin_end

//tab_start
$(function () {
    $("[benlai-effect=tab]").Tab();
});
//tab_end

//date-start-end
$(function() {
    var now = new Date();
    $('[data-start], [data-end]').each(function() {
        var start = $.trim($(this).attr('data-start'));
        var end = $.trim($(this).attr('data-end'));
        if (start && !end) {
            var start_time = new Date(start);
            $(this).toggle(now > start_time);
            return;
        }
        if (start && end) {
            var start_time = new Date(start);
            var end_time = new Date(end);
            $(this).toggle(now > start_time && now < end_time);
            return;
        }
        if (!start && end) {
            var end_time = new Date(end);
            $(this).toggle(now < end_time);
            return;
        }
    });
});
//date-start-end

//calculate_discount_start
$(function() {
    $.fn.calcDiscount = function(option) {
        var o = $.extend({
            temp: '直降{0}元',
            price: '[data-module="price"]',
            word: '[data-module="word"]'
        }, option || {});
        $(this).each(function() {
            var price = $(o.price, this).html().match(/￥\d*\.\d*/gi);
            if (!price || price.length != 2) {
                return;
            }
            var cP = price[0].split("￥")[1];
            var oP = price[1].split("￥")[1];
            var discountPrice = parseFloat(oP) - parseFloat(cP);
            var result = $.BENLAI.format(o.temp, discountPrice.toFixed(2));
            $(o.word, this).html(result).show();
        });
    };
    $('[benlai-effect="calcDiscount"]').calcDiscount();
});
//calculate_discount_end

//benlai yeshi
$(function(){
    $('body').CreateController({
        events:{
        },
        initData:function(that){
            var currentDate=new Date(),
             year=currentDate.getFullYear(),
             month=currentDate.getMonth()+1,
             day=currentDate.getDate();
             that.currentDateStr=year+"/"+month+"/"+day;
                    that.ctime=new Date(that.currentDateStr).getTime();

             that.tabCon=$("[effect-tab='content']");
             that.navLst=$("[effect-tab='nav']").children();

             that.navLen=that.navLst.length;             
             that.len=that.tabCon.children().length/that.navLen;
             for(var i=1;i<=that.len;i++){
                var temp="[data-type=ys-skill"+i+"]",
                               tempTime=new Date(that.tabCon.children(temp).eq(0).attr("date")).getTime();
                if(that.ctime==tempTime){
                    that.skuList=that.tabCon.children(temp);
                }else{
                               that.tabCon.children(temp).remove();
                         }
             }  
                    if(!that.skuList) return;
                    that.btnList=that.skuList.find(".btn");
                    that.s=[];
                    for(var i=0;i<that.navLen;i++){
                        that.s.push(new Date(that.currentDateStr+" "+that.skuList.eq(i).attr("start-time")).getTime());
                        that.s.push(new Date(that.currentDateStr+" "+that.skuList.eq(i).attr("end-time")).getTime());
                    }
        },
        showDjs:function(){
            var that=this;
                   if(!that.skuList) return;
            var curTime=new Date().getTime();
            that.skuList.each(function(i){
                var _this_=this,
                startDate=that.currentDateStr+" "+$(this).attr("start-time"),
                endDate=that.currentDateStr+" "+$(this).attr("end-time"),
                startTime=new Date(startDate),
                endTime=new Date(endDate),
                str="",
                tempDate="",
                po0=$(_this_).find(".pointOut").eq(0),
                po1=$(_this_).find(".pointOut").eq(1);
                if(curTime>=startTime&&curTime<=endTime){
                    po0.html("活动进行中");
                    str="距离本场结束";
                    tempDate=endDate;
                }else if(curTime<startTime){
                    po0.html("活动未开始");
                    str="距离本场开始";
                    tempDate=startDate;
                }else{
                    po0.html("活动已结束");
                    po1.hide();
                }
                $.BENLAI.countDown(
                { 
                                    dom: po1,
                                    date: new Date(tempDate),
                                    temp: str+'<span class="red" >{hour}:{minute}:{second}</span>',
            callback: function () {
                                location.reload();
            }
        }); 
            });
        },
        autoTab:function(that){
                   if(!that.s){return;}
            var curTime=new Date().getTime();
            var slen=that.s.length;
            if(curTime>=that.s[slen-2]){                                 
                that.navLst.eq(that.navLen-1).trigger("click");
            }else if(curTime<=that.s[1]){
                that.navLst.eq(0).trigger("click");
            }else{
                    for(var j=2;j<slen-2;j=j+2)
                    {
                        if(that.s[j]<=curTime&&that.s[j+1]>=curTime){
                            that.navLst.eq(Math.floor(j/2)).trigger("click");
                        }
                    }
            }
            that.startIndex=that.navLst.index(".on");
                  that.switchBtn(that);
        },
                        switchBtn:function(that){
                                   var curTime=new Date().getTime();
                                   var slen=that.s.length;
                                   var ul_Lth=that.btnList.length/that.navLen;
                                   that.btnList.each(function(i){               
                                 if(curTime<that.s[0]){                                                  
                                     that.btnList.eq(i).children("[btn-status='begin']").show().siblings().hide();
                                 }else if(curTime>that.s[slen-1]){                                              
                                     that.btnList.eq(i).children("[btn-status='gmout']").show().siblings().hide();
                                 }else{                                                     
                                    for(var j=0;j<=slen-2;j=j+2){
                                            if(curTime>=that.s[j]&&curTime<=that.s[j+1]){
                                                    //that.navLst.find(".on").index()
                                                    if(curTime>=that.s[0]&&curTime<=that.s[1]){
                                                        if(i<ul_Lth){
                                                            that.btnList.eq(i).children("[btn-status='begin']").hide();
                                                        }else{
                                                            that.btnList.eq(i).children("[btn-status='begin']").show().siblings().hide();
                                                        }
                                                    }else if(curTime>=that.s[slen-2]&&curTime<=that.s[slen-1]){
                                                        if(i<ul_Lth*(that.navLen-1)){
                                                            that.btnList.eq(i).children("[btn-status='gmout']").show().siblings().hide();
                                                        }else{
                                                            that.btnList.eq(i).children("[btn-status='begin']").hide();
                                                        }
                                                    }else{
                                                            if(i<ul_Lth*that.startIndex){
                                                                   that.btnList.eq(i).children("[btn-status='gmout']").show().siblings().hide();
                                                            }else if(i>=ul_Lth*(that.startIndex+1)){
                                                                that.btnList.eq(i).children("[btn-status='begin']").show().siblings().hide();                            
                                                            }else{
                                                                  that.btnList.eq(i).children("[btn-status='begin']").hide(); 
                                                            } 
                                                    }
                                            }
                                        }                                                                                            
                                      }
                                    });
                        },
        init:function(){
            var that=this;
            this.initData(that); 
            setTimeout(function(){
                that.autoTab(that);
            },100);
            this.showDjs();                                  
        }
    });
});
//benlai yeshi_end